/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

// import { Alert, Button } from 'antd';
import i18n from 'i18next';

import { ICmdDialogParams } from '@amcax/base';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { wsApi } from '@app-cad/server';
import { CurvedBaseCommand } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import { useState } from 'react';

const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 24 },
};

type FormDataType = {
  name?: string;
};

function GeomBoxDialog({ title, onClose }: ICmdDialogParams) {
  const [formData, setFormData] = useState<FormDataType>();
  const [isLoading, setIsLoading] = useState(false);

  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      name: {
        type: 'string',
        title: '名称',
      },
    },
  };

  const finishDraw = async () => {
    setIsLoading(true);
    try {
      await wsApi.createGeomBox(formData.name);
    } finally {
      setIsLoading(false);
    }
    onClose();
  };

  useCommand('geomBox', {
    onClose,
    finishDraw,
  });

  return (
    <PickableCommandForm
      onClose={onClose}
      title={title}
      formContext={formItemLayout}
      schema={schema}
      formData={formData}
      submitLoading={isLoading}
      onChange={(v) => {
        setFormData(v);
      }}
      extraUiSchema={{
        name: {
          'ui:placeholder': '请输入名称',
          'ui:autofocus': true,
          'ui:autocomplete': 'off',
        },
      }}
      submitText='完成'
      onSubmit={finishDraw}
    />
  );
}

export const cmdGeomBox: CurvedBaseCommand = {
  id: 'CmdGeomBox',
  label: '创建几何体',
  icon: 'commands/curved/geomBox',
  create: (label, onClose) => (
    <GeomBoxDialog
      title={i18n.t(label)}
      onClose={onClose}
    />
  ),
};
